﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RiadenieProjektov
{
    public partial class frmUzivatelia : Form
    {
        #region properties
        private RiadenieProjektov.KAkcia aAkcia;
        public RiadenieProjektov.KAkcia akcia
        {
            get { return aAkcia; }
            set { aAkcia = value; }
        }

        private int aUzivateliaId;
        public int uzivateliaId
        {
            get { return aUzivateliaId; }
            set { aUzivateliaId = value; }
        }

        private RiadenieProjektov.KChyba aResult = KChyba.KonNezmenene;
        public RiadenieProjektov.KChyba result
        {
            get { return aResult; }
            set { aResult = value; }
        }
        #endregion

        public frmUzivatelia()
        {
            InitializeComponent();
        }

        public void naplnitPolia()
        {
            this.pozicieTableAdapter.Fill(this.dsVsetko.pozicie);
            this.uzivateliaTableAdapter.Fill(this.dsVsetko.uzivatelia);
            if (akcia == RiadenieProjektov.KAkcia.KonPridat)
            {
                DataRow newRow = this.dsVsetko.uzivatelia.NewRow();
                newRow["pozicieId"] = this.dsVsetko.pozicie[0].pozicieId;
                newRow["menoPriezvisko"] = "";
                newRow["mzdaNaHod"] = 0;
                newRow["prihlasovacieMeno"] = "";
                newRow["heslo"] = "";
                newRow["rodneCislo"] = "";
                newRow["cisloUctu"] = "";
                newRow["poznamka"] = "";
                this.dsVsetko.uzivatelia.Rows.Add(newRow);
                uzivateliaBindingSource.MoveLast();

                btnUlozit.Enabled = true;
                btnZmazat.Enabled = false;
                tlacidla(false);
            }
            else if (akcia == RiadenieProjektov.KAkcia.KonUpravit)
            {
                uzivateliaBindingSource.Position = uzivateliaBindingSource.Find("uzivateliaId", uzivateliaId);
                btnUlozit.Enabled = true;
                btnZmazat.Enabled = false;
                tlacidla(true);
            }
            else if (akcia == RiadenieProjektov.KAkcia.KonVymazat)
            {
                uzivateliaBindingSource.Position = uzivateliaBindingSource.Find("uzivateliaId", uzivateliaId);
                btnUlozit.Enabled = false;
                btnZmazat.Enabled = true;
                tlacidla(false);
            }
            else
            {
                Console.WriteLine("Nenastavená akcia.");
            }
        }

        private void tlacidla(bool viditelne)
        {
            btnPrvy.Enabled = viditelne;
            btnPredosly.Enabled = viditelne;
            btnDalsi.Enabled = viditelne;
            btnPosledny.Enabled = viditelne;
        }

        private void frmUzivatelia_Load(object sender, EventArgs e)
        {
        }

        private void btnZavriet_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnUlozit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Validate();
                this.uzivateliaBindingSource.EndEdit();
                this.tableAdapterManager.UpdateAll(dsVsetko);
                MessageBox.Show("Záznam bol úspešne upravený.", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.result = KChyba.KonZmenene;

            }
            catch (Exception tt)
            {
                Console.WriteLine(tt.Message);
                MessageBox.Show("Chyba, záznam nebol upravený. " + tt.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.result = KChyba.KonNezmenene;
            }
            Close();
        }

        private void CurrentChanged(object sender, EventArgs e)
        {
            Console.WriteLine("CurrentChanged");
        }

        private void CurrentItemChanged(object sender, EventArgs e)
        {
            Console.WriteLine("CurrentItemChanged");
        }

        private void DataMemberChanged(object sender, EventArgs e)
        {
            Console.WriteLine("DataMemberChanged");
        }

        private void DataSourceChanged(object sender, EventArgs e)
        {
            Console.WriteLine("DataSourceChanged");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //dsVsetko.uzivatelia.FindByuzivateliaId(uzivateliaId);            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            uzivateliaBindingSource.Position = 2;
        }

        private void btnPredosly_Click(object sender, EventArgs e)
        {
            uzivateliaBindingSource.MovePrevious();
        }

        private void btnPrvy_Click(object sender, EventArgs e)
        {
            uzivateliaBindingSource.MoveFirst();
        }

        private void btnDalsi_Click(object sender, EventArgs e)
        {
            uzivateliaBindingSource.MoveNext();
        }

        private void btnPosledny_Click(object sender, EventArgs e)
        {
            uzivateliaBindingSource.MoveLast();
        }
        
        private void btnZmazat_Click(object sender, EventArgs e)
        {
            try
            {
                uzivateliaBindingSource.RemoveCurrent();
                this.tableAdapterManager.UpdateAll(dsVsetko);
                MessageBox.Show("Záznam bol úspešne zmazaný.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.result = KChyba.KonZmenene;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Chyba, záznam nebol zmazaný. " + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.result = KChyba.KonNezmenene;
            }
            Close();
        }
    }
}
